package org.aeon.gamechatnest.common.response;

import org.aeon.gamechatnest.exception.ApplicationException;

import java.util.Objects;

/**
 * @author: MissingAeon
 * @date: 2024/4/18 14:31
 * @description:
 */
public class ResultBuild {
    // 线程变量，用于在多线程环境下传递结果
    private static final ThreadLocal<ResultBuild> localResult = new ThreadLocal<>();
    private RespStatus respStatus;
    private String message;
    private Object data;
    private Long timestamp;

    /**
     * 创建返回结果
     * 先查看本线程是否存在返回结果，存在则返回，不存在则创建
     *
     * @return
     */
    public static ResultBuild result() {
        if (Objects.isNull(localResult.get())) {
            ResultBuild resultBuild = new ResultBuild();
            localResult.set(resultBuild);
            return resultBuild;
        }
        return localResult.get();
    }

    /**
     * 返回是否成功的结果（不带数据）
     *
     * @param condition 条件
     * @return this
     */
    public static ResultBuild successIf(boolean condition) {
        return condition ? result().successful() : result().failed();
    }

    /**
     * 返回是否成功的结果（带数据）
     *
     * @param condition 条件
     * @param data      数据
     * @return this
     */
    public static ResultBuild successIf(boolean condition, Object data) {
        return condition ? result().successful().data(data) : result().failed().data(data);
    }

    /**
     * 根据条件成功返回结果，失败抛错
     *
     * @param condition            条件
     * @param applicationException 要抛出的错误
     * @return this
     */
    public static ResultBuild throwAnErrorIf(boolean condition, ApplicationException applicationException) {
        if (condition) {
            return result().successful();
        } else {
            throw applicationException;
        }
    }

    /**
     * 成功状态
     *
     * @return
     */
    public ResultBuild successful() {
        respStatus = RespStatus.SUCCESS;
        return this;
    }

    /**
     * 失败状态
     *
     * @return
     */
    public ResultBuild failed() {
        respStatus = RespStatus.FAIL;
        return this;
    }

    /**
     * 错误状态
     *
     * @return
     */
    public ResultBuild throwAnError() {
        respStatus = RespStatus.ERROR;
        return this;
    }

    /**
     * 状态
     *
     * @param state
     * @return
     */
    public ResultBuild state(RespStatus state) {
        respStatus = state;
        return this;
    }

    /**
     * 特殊信息
     *
     * @param message
     * @return
     */
    public ResultBuild message(String message) {
        this.message = message;
        return this;
    }

    /**
     * 设置返回结果
     *
     * @param data
     * @return
     */
    public ResultBuild data(Object data) {
        this.data = data;
        return this;
    }

    /**
     * 设置时间戳
     *
     * @param timestamp
     * @return
     */
    public ResultBuild timestamp(Long timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    /**
     * 生成时间戳
     *
     * @return
     */
    public ResultBuild timestamp() {
        timestamp = System.currentTimeMillis();
        return this;
    }

    /**
     * 构建返回结果
     *
     * @return
     */
    public Result build() {
        if (respStatus == null) {
            throw new RuntimeException("状态不能为空");
        }
        Result result = new Result();
        result.setCode(respStatus.getCode());
        result.setMessage(respStatus.getMessage());
        result.setData(data);
        result.setTimestamp(timestamp);
        if (message != null) {
            result.setMessage(message);
        }
        // 清空
        localResult.remove();
        return result;
    }

}
